//
//  XDDetViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/24.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "XDDetViewController.h"

#import "LoginViewController.h"
#import "NavViewController.h"

#import "PayForViewController.h"
#import "XDDetCell.h"
@interface XDDetViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *array;
}
@property (nonatomic,copy)NSString *str;
@property (nonatomic,strong)UITableView *tableView;

@end

@implementation XDDetViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setBGColor];
    [self showNav:@"信贷详情"];
    array = [NSMutableArray array];
    
    [self loadUI];
    [self loadNum];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadData];
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 60)];
    _tableView.tableHeaderView = headView;
    
    UILabel *labBank = [[UILabel alloc] initWithFrame:CGRectMake(16, 16, 30, 17)];
    labBank.text = _bank;
    labBank.textAlignment = NSTextAlignmentCenter;
    labBank.textColor = COLOR_RGB(65, 132, 255, 1.0);
    labBank.font = [UIFont systemFontOfSize:11.0];
    labBank.layer.borderWidth = 1.0;
    labBank.layer.cornerRadius = 5.0;
    labBank.layer.borderColor = COLOR_RGB(65, 132, 255, 1.0).CGColor;
    [headView addSubview:labBank];
    
    UILabel *labTit = [[UILabel alloc] initWithFrame:CGRectMake(55, 16, 200, 17)];
    labTit.text = [NSString stringWithFormat:@" %@",_tit];
    labTit.font = [UIFont systemFontOfSize:16.0];
    [headView addSubview:labTit];
    
    UILabel *labDate = [[UILabel alloc] initWithFrame:CGRectMake(16, 45, 100, 17)];
    labDate.text = _date;
    labDate.font = [UIFont systemFontOfSize:13.0];
    labDate.textColor = [UIColor lightGrayColor];
    labDate.font = [UIFont systemFontOfSize:16.0];
    [headView addSubview:labDate];
    
    UIButton *btnL = [UIButton buttonWithType:UIButtonTypeCustom];
    btnL.frame = CGRectMake(ScreenWidth-60-40, 45, 45, 20);
    [btnL setTitle: [NSString stringWithFormat:@" %@",_watch] forState:UIControlStateNormal];
    [btnL setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btnL.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [btnL setImage:[UIImage imageNamed:@"浏览2"] forState:UIControlStateNormal];
    [headView addSubview:btnL];
    
    UIButton *btnR = [UIButton buttonWithType:UIButtonTypeCustom];
    btnR.frame = CGRectMake(ScreenWidth-60, 45, 45, 20);
    [btnR setTitle: [NSString stringWithFormat:@" %@",_pay] forState:UIControlStateNormal];
    [btnR setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btnR.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [btnR setImage:[UIImage imageNamed:@"费用支付单"] forState:UIControlStateNormal];
    [headView addSubview:btnR];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(16, labDate.frame.origin.y+labDate.frame.size.height+12, ScreenWidth-32, 1)];
    line.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [headView addSubview:line];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cellid";
    XDDetCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"XDDetCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dic = array[indexPath.row];
    NSString *is_pay = [dic objectForKey:@"is_pay"];//2收费3不收费
    NSString *is_pic = [dic objectForKey:@"is_pic"];//是否是图片2是3否
    NSString *is_cancel = [dic objectForKey:@"is_cancel"];//2已收藏3未收藏
    NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[dic objectForKey:@"text"]];
    NSString *text = [dic objectForKey:@"text"];
    if ([is_pic integerValue] == 2) {//是图片
        cell.labels.hidden = YES;
        [cell.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@""]];
        if ([is_pay integerValue] == 2) {//2收费
            cell.images.hidden = YES;
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(20, 20, ScreenWidth-40, 45);
            [btn setImage:[UIImage imageNamed:@"组-8"] forState:UIControlStateNormal];
            btn.tag = indexPath.row;
            [btn addTarget:self action:@selector(clickText:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:btn];
            
        }else{
            cell.images.hidden = NO;
        }
        
    }else{
        cell.labels.text = text;
        cell.images.hidden = YES;
        
        
        
        if ([is_pay integerValue] == 2) {
            cell.labels.hidden = YES;
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(20, 20, ScreenWidth-40, 50);
            [btn setImage:[UIImage imageNamed:@"组-8"] forState:UIControlStateNormal];
            btn.tag = indexPath.row;
            [btn addTarget:self action:@selector(clickText:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:btn];
        }else{
            cell.labels.hidden = NO;
        }
    }
    
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = array[indexPath.row];
    NSString *is_pay = [dic objectForKey:@"is_pay"];//2收费3不收费
    if ([is_pay integerValue] == 2) {
        return 80.0;
    }
    
    NSString *text = [dic objectForKey:@"text"];
    NSString *is_pic = [dic objectForKey:@"is_pic"];
    if ([is_pic integerValue] == 2) {
        return 200.0;
    }else{
        CGSize size = [text sizeWithFont:[UIFont boldSystemFontOfSize:17.0f] constrainedToSize:CGSizeMake(self.view.frame.size.width, 300) lineBreakMode:NSLineBreakByWordWrapping];
        NSLog(@"height:%.2f",size.height);
        return size.height+20.0;
    }
    
}

//
- (void)clickText:(UIButton *)btn{
    NSInteger count = btn.tag;
    NSDictionary *dic = array[count];
    NSString *xdid = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"id"] integerValue]];
    
    NSString *strs = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:strs];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:token forKey:@"verifiToken"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setObject:_str forKey:@"ordernumber"];
    [parmas setObject:_tit forKey:@"ordername"];
    [parmas setObject:_xdid forKey:@"creditid"];
    [parmas setObject:@"2" forKey:@"payment"];
    [parmas setObject:@"1" forKey:@"paymoney"];
    [parmas setObject:xdid forKey:@"payid"];//段落id
    [parmas setObject:@"3" forKey:@"balance"];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"因平台运营需要和防止恶意人员获取信息，现收取1元手续费，谢谢您的支持，查看后可以收藏此教程！" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"去支付" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        PayForViewController *pay = [[PayForViewController alloc] init];
        pay.dic = parmas;
        pay.OrderName = @"查看信贷教程付费段落";
        pay.price = @"1";
        [self.navigationController pushViewController:pay animated:YES];
    }];
    
    [alert addAction:action1];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
}

//收藏
- (void)clickSC:(UIButton *)btn{
    if (![user objectForKey:@"userid"]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        NavViewController *nav = [[NavViewController alloc] initWithRootViewController:login];
        nav.navigationBarHidden = YES;
        [self presentViewController:nav animated:YES completion:nil];
        return;
    }
    
    if (btn.tag == 1) {
        [btn setImage:[UIImage imageNamed:@"收-藏-大-拷贝-2"] forState:UIControlStateNormal];
        btn.tag = 2;
    }else{
        [btn setImage:[UIImage imageNamed:@"收-藏-大"] forState:UIControlStateNormal];
        btn.tag = 1;
    }
}

//生成订单号
- (void)loadNum{
    NSString *url = [NSString stringWithFormat:@"%@/App/Shoppingcart/ordernumber",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"生成订单号res:%@",responseObject);
        _str = [responseObject objectForKey:@"result"];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}
//信贷详情
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setValue:_xdid forKey:@"id"];
    [parmas setValue:[user objectForKey:@"userid"] forKey:@"userid"];
    NSLog(@"parmas:%@",parmas);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@/App/Credit/details",urlHead];
    [manager GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"信贷详情res:%@",responseObject);
        array = [responseObject objectForKey:@"result"];
        
        [_tableView reloadData];
        
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
